/**
 * @author LKQ
 * @date 2021/11/28 14:47
 * @description 单层循环，只要不匹配，那么就重新开始循环。
 */
public class Solution1 {
    public static void main(String[] args) {
        Solution1 solution = new Solution1();
        String[] str = {"cir","car"};
        System.out.println(solution.longestCommonPrefix(str));
    }

    public String longestCommonPrefix(String[] strs) {
        String shortest = strs[0];
        // 找到最短的字符串
        for (String str : strs) {
            if (str.length() < shortest.length()) {
                shortest = str;
            }
        }
        int length = shortest.length();
        for (int i = strs.length - 1; i >= 0; i--) {
            if (!strs[i].startsWith(shortest)) {
                shortest = shortest.substring(0, length-1);
                if (length==1) {
                    shortest = "";
                }
                length--;
                i = strs.length;
            }
        }

        return shortest;
    }
}
